﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;

public partial class school_period : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //int _startYear = DateTime.Now.Year - 5;
            //int _curYear = DateTime.Now.Year;
            //int _endYear = DateTime.Now.Year + 1;

            //for (int i = _startYear; i <= _endYear; i++)
            //{
            //    ddlStartYear.Items.Add(i.ToString("0000"));
            //    ddlEndYear.Items.Add(i.ToString("0000"));
            //}

            //ddlStartYear.Text = _curYear.ToString();
            //ddlEndYear.Text = _curYear.ToString();

            if (DateTime.Now.Year >= 2012)
            {
                for (int y = 2012; y <= DateTime.Now.Year + 3; y++)
                {
                    ddlPeriodStartInYear.Items.Add(y.ToString());
                }

                ddlPeriodStartInYear.Text = DateTime.Now.Year.ToString();
            }
            else
            {
                for (int y = DateTime.Now.Year; y <= DateTime.Now.Year + 3; y++)
                {
                    ddlPeriodStartInYear.Items.Add(y.ToString());
                }

                ddlPeriodStartInYear.Text = DateTime.Now.Year.ToString();
            }

            SIS.Utilities.General.LoadDayDropdownList(ddlStartDay);
            SIS.Utilities.General.LoadMonthDropdownList(ddlStartMonth, 2);
            SIS.Utilities.General.LoadYearDropdownList(ddlStartYear, (DateTime.Now.Year - 5), DateTime.Now.Year + 5, 1);

            SIS.Utilities.General.LoadDayDropdownList(ddlEndDay);
            SIS.Utilities.General.LoadMonthDropdownList(ddlEndMonth, 2);
            SIS.Utilities.General.LoadYearDropdownList(ddlEndYear, (DateTime.Now.Year - 5), DateTime.Now.Year + 5, 1);

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }

            ClearFields();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime dtStartDate = new DateTime(Convert.ToInt32(ddlStartYear.Text), Convert.ToInt32(ddlStartMonth.Text), Convert.ToInt32(ddlStartDay.Text));
        DateTime dtEndDate = new DateTime(Convert.ToInt32(ddlEndYear.Text), Convert.ToInt32(ddlEndMonth.Text), Convert.ToInt32(ddlEndDay.Text));

        if (DateTime.Compare(dtStartDate, dtEndDate) >= 0)
        {
            lblMessage.Text = "Invalid start and end period date.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            return;
        }

        Page.Validate(btnSubmit.ValidationGroup);

        if (Page.IsValid)
        {
            EducationPeriodTable _period = new EducationPeriodTable();
            _period.EducationPeriodKey = Convert.ToInt32(lblEducationPeriodKey.Text);
            _period.EducationPeriodCode = txtEducationPeriodCode.Text;
            _period.Description = txtDescription.Text;
            _period.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
            _period.LocationKey = Convert.ToInt32(ddlLocation.Text);
            _period.StandardLevelKey = Convert.ToInt32(ddlStandardLevel.Text);
            _period.StartDate = dtStartDate;
            _period.EndDate = dtEndDate;

            _period.Status = chkStatus.Checked;
            int _periodKey = EducationPeriodTableManager.Save(_period);
            if (_periodKey > 0)
            {
                lblEducationPeriodKey.Text = _periodKey.ToString();
                lblMessage.Text = "Saved successfully!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
                grvEduPeriod.DataBind();
                ClearFields();
            }
            else
            {
                lblMessage.Text = "Saving failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void btnShowSchoolProfile_Click(object sender, EventArgs e)
    {

    }

    protected void btnAddNewSchool_Click(object sender, EventArgs e)
    {

    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }

    private void ClearFields()
    {
        lblMessage.Text = "";
        lblSchoolKey.Text = ddlSchoolList.Text;
        lblEducationPeriodKey.Text = "-1";
        txtEducationPeriodCode.Text = "";
        txtDescription.Text = "";
        ddlStartYear.Text = DateTime.Now.Year.ToString("0000");
        ddlStartYear.Enabled = false;
        ddlEndYear.Text = DateTime.Now.Year.ToString("0000");
        ddlStartMonth.Text = DateTime.Now.Month.ToString("00");
        ddlEndMonth.Text = DateTime.Now.Month.ToString("00");
        chkStatus.Checked = false;
    }

    protected void grvEduPeriod_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();

        if (grvEduPeriod.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvEduPeriod.SelectedDataKey;
            int _eduPeriodKey = Convert.ToInt32(_selectedDataKey[0]);
            int _schoolKey = Convert.ToInt32(_selectedDataKey[1]);
            //Response.Write(_schoolKey.ToString());
            if (_eduPeriodKey > 0 && _schoolKey > 0)
            {
                lblSchoolKey.Text = _schoolKey.ToString();
                lblEducationPeriodKey.Text = _eduPeriodKey.ToString();

                EducationPeriodTable _eduPeriod = new EducationPeriodTable();
                _eduPeriod = EducationPeriodTableManager.GetItem(Convert.ToInt32(lblEducationPeriodKey.Text));

                if (_eduPeriod != null)
                {
                    txtEducationPeriodCode.Text = _eduPeriod.EducationPeriodCode;
                    txtDescription.Text = _eduPeriod.Description;

                    DateTime _dtStartDate = _eduPeriod.StartDate;
                    DateTime _dtEndDate = _eduPeriod.EndDate;

                    ddlStartYear.Text = _dtStartDate.Year.ToString("0000");
                    ddlEndYear.Text = _dtEndDate.Year.ToString("0000");

                    ddlStartMonth.Text = _dtStartDate.Month.ToString("00");
                    ddlEndMonth.Text = _dtEndDate.Month.ToString("00");

                    ddlStartDay.Text = _dtStartDate.Day.ToString("00");
                    ddlEndDay.Text = _dtEndDate.Day.ToString("00");

                    chkStatus.Checked = _eduPeriod.Status;
                }

                //GridViewRow _selectedRow = grvEduPeriod.SelectedRow;
                //txtEducationPeriodCode.Text = _selectedRow.Cells[4].Text;
                //txtDescription.Text = _selectedRow.Cells[5].Text;

                //DateTime _dtStartDate = Convert.ToDateTime(_selectedRow.Cells[6].Text);
                //DateTime _dtEndDate = Convert.ToDateTime(_selectedRow.Cells[7].Text);

                //ddlStartMonth.Text = _dtStartDate.Month.ToString();
                //ddlEndMonth.Text = _dtEndDate.Month.ToString();

                //if (ddlStartYear.Items.FindByValue(_dtStartDate.Year.ToString("0000")) != null)
                //{
                //    ddlStartYear.Text = _dtStartDate.Year.ToString("0000");
                //}
                //if (ddlEndYear.Items.FindByValue(_dtEndDate.Year.ToString("0000")) != null)
                //{
                //    ddlEndYear.Text = _dtEndDate.Year.ToString("0000");
                //}

                //CheckBox _chkStatus = (CheckBox)(_selectedRow.Cells[8].Controls[0]);
                //chkStatus.Checked = _chkStatus.Checked;
            }
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblEducationPeriodKey.Text.Length > 0)
        {
            if (EducationPeriodTableManager.Delete(Convert.ToInt32(lblEducationPeriodKey.Text)))
            {
                ClearFields();
                grvEduPeriod.DataBind();
                lblMessage.Text = "Item was deleted!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Deleting failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please select an item to delete.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvEduPeriod.DataBind();
        ClearFields();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvEduPeriod.DataBind();
        ClearFields();
    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlStandardLevel.DataBind();
        ddlStandardLevel_SelectedIndexChanged(sender, e);
        ddlLocation.DataBind();
        ddlLocation_SelectedIndexChanged(sender, e);
        grvEduPeriod.DataBind();
        ClearFields();
    }

    protected void ddlPeriodStartInYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvEduPeriod.DataBind();
        ClearFields();
        ddlStartYear.Text = ddlPeriodStartInYear.Text;
        ddlEndYear.Text = ddlPeriodStartInYear.Text;
    }
}
